package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.exception.ServiceException;
//import com.ruoyi.common.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RealtestMapper;
import com.ruoyi.system.domain.Realtest;
import com.ruoyi.system.service.IRealtestService;

import java.util.List;

/**
 * realtestService业务层处理
 *
 * @author ruoyi
 * @date 2025-03-21
 */
@Service
public class RealtestServiceImpl implements IRealtestService {
    private static final Logger log = LoggerFactory.getLogger(RealtestServiceImpl.class);

    @Autowired
    private RealtestMapper realtestMapper;

    /**
     * 查询realtest
     *
     * @param ywid realtest主键
     * @return realtest
     */
    @Override
    public Realtest selectRealtestByYwid(Long ywid) {
        return realtestMapper.selectRealtestByYwid(ywid);
    }

    /**
     * 查询realtest列表
     *
     * @param realtest realtest
     * @return realtest集合
     */
    @Override
    public List<Realtest> selectRealtestList(Realtest realtest) {
        return realtestMapper.selectRealtestList(realtest);
    }

    /**
     * 新增realtest
     *
     * @param realtest realtest
     * @return 结果
     */
    @Override
    public int insertRealtest(Realtest realtest) {
        realtest.setCreateTime(DateUtils.getNowDate());
        return realtestMapper.insertRealtest(realtest);
    }

    /**
     * 修改realtest
     *
     * @param realtest realtest
     * @return 结果
     */
    @Override
    public int updateRealtest(Realtest realtest) {
        realtest.setUpdateTime(DateUtils.getNowDate());
        return realtestMapper.updateRealtest(realtest);
    }

    /**
     * 批量删除realtest
     *
     * @param ywids 需要删除的realtest主键集合
     * @return 结果
     */
    @Override
    public int deleteRealtestByYwids(Long[] ywids) {
        return realtestMapper.deleteRealtestByYwids(ywids);
    }

    /**
     * 删除realtest信息
     *
     * @param ywid realtest主键
     * @return 结果
     */
    @Override
    public int deleteRealtestByYwid(Long ywid) {
        return realtestMapper.deleteRealtestByYwid(ywid);
    }

    /**
     * 导入realtest数据
     *
     * @param realtestList 要导入的realtest数据列表
     * @param updateSupport 是否支持更新已存在的数据
     * @param operName 操作人姓名
     * @return 导入结果信息
     */
    @Override
    public String importRealtest(List<Realtest> realtestList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(realtestList) || realtestList.size() == 0) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<Realtest> existList = selectRealtestList(null);
        for (Realtest importData : realtestList) {
            try {
                boolean realtestFlag = false;
                for (Realtest entry : existList) {
                    // 假设 ywid 是唯一标识，可根据实际情况修改
                    if (entry.getYwid().equals(importData.getYwid())) {
                        realtestFlag = true;
                        break;
                    }
                }
                if (!realtestFlag) {
                    insertRealtest(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getYwid() + " 导入成功");
                } else if (updateSupport) {
                    updateRealtest(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getYwid() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getYwid() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据 " + importData.getYwid() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            // throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
